// -----------------------------------------------------------------------------
// quick simple
// -----------------------------------------------------------------------------

// -----------------------------------------------------------------------------
// functions
// -----------------------------------------------------------------------------
function changeNumArr(){
  var reArr = [0,''];
  var reNullArr = [0,''];
  var str = '';
  var oChkP = $('#tblQPG01 input:checkbox:checked');
  var oChkG = $('#tblQPG02 input:checkbox:checked');
  var oChkT = $('#tblQPG03 input:checkbox:checked');
  var oChkO = $('#tblQPG04 input:checkbox:checked');
  var oChkN = $('#tblQPG05 input:checkbox:checked');
  var oChkL = $('#tblQPG06 input:checkbox:checked');
  var oChkPL = oChkP.length;
  var oChkGL = oChkG.length;
  var oChkTL = oChkT.length;
  var oChkOL = oChkO.length;
  var oChkNL = oChkN.length;
  var oChkLL = oChkL.length;
  var i = 0; var j = 0; var k = 0; var n = 0; var p = ''; var o = ''; var n = ''; var strArr = ''; var eachNum = $('#eachNum').val() - 0;
  str = '<tr>';
  var chkArr = new Array(); // chk two face
  var chkStr = ',,'; // chk two face  
                             
  if(oChkLL > 0 && twoLongVal.length > 0){
    var tow_longMax = $('#tow_longMax').val();
    var towLongIdxArr = '';
    for(i = 0; i < twoLongVal.length; i++){
      if(twoLongVal[i] >= tow_longMax){
        if(n > 0 && n % eachNum == 0){ str += '</tr><tr>'; }  // count tr
        towLongIdxArr = twoLongIdx[i].split('_'); // ball(bettype)_subid_number
        n++;
        str += '<td class="onNum">' + getBetNumName(towLongIdxArr[1], ( towLongIdxArr[0] + '_' + towLongIdxArr[2] ) ) + '</td>';
        chkArr.push(towLongIdxArr[1] + '_' + towLongIdxArr[0] + '_' + towLongIdxArr[2]); 
      }
    }
    if( n > 0 ){ chkStr = ',' + chkArr.toString() + ','; }
  }
                 
  for(i = 0; i < oChkPL ; i++){
    p = oChkP[i].value;
    for(j = 0; j < oChkGL ; j++){
      if( chkStr.search( ',' + p + '_' + oChkG[j].value + ',' ) > -1 ){ continue; }
      if(n > 0 && n % eachNum == 0){ str += '</tr><tr>'; }  // count tr
      n++;
      str += '<td class="onNum">' + getBetNumName(p, oChkG[j].value) + '</td>';
    }
  }
  for(k = 0; k < oChkTL ; k++){
    if( chkStr.search( ',0_' +  oChkT[k].value + ',' ) > -1 ){ continue; }
    if(n > 0 && n % eachNum == 0){ str += '</tr><tr>'; }  // count tr
    n++;
    str += '<td class="onNum">' + getBetNumName(0, oChkT[k].value) + '</td>';
  }
  for(i = 0; i < oChkOL ; i++){
    o = oChkO[i].value;
    for(j = 0; j < oChkNL ; j++){
      if( chkStr.search( ',' +  o + '_' + oChkN[j].value + ',' ) > -1 ){ continue; }
      if(n > 0 && n % eachNum == 0){ str += '</tr><tr>'; }  // count tr
      n++;
      str += '<td class="onNum">' + getBetNumName(o, oChkN[j].value) + '</td>';
    }
  }

  
  str += '</tr>';
  if(n > 0){ return [n, str]; }
  else{ return reNullArr; }
}

$(function(){

  
  $(".tblQPG input:checkbox").click(function(){
    $(this).parent('label').toggleClass("noClk");
  }); 
  
  // proc quick data
  $("#btnProcData").click(function(){
    $(this).attr("disabled", "disabled");
    var strArr = changeNumArr();
    var l = strArr[0];
    $("#totalNumCount").text(0);
    if(l){
      $("#tbdShowNumber").html(strArr[1]);
      $("#tbdShowNumber td").click(function(){
        $(this).toggleClass("onNum");
        var onNumLen = $("#tbdShowNumber td.onNum").length;
        var sendMonNumTotal = $('#setMomney').val() * 1 * onNumLen;
        $("#sendNumTotal").text(onNumLen);
        $("#sendMonNumTotal").text(sendMonNumTotal);
      });
      $("#totalNumCount").text(l);
      $("#sendNumTotal").text(l);
      $("#sendMonNumTotal").text(($('#setMomney').val() * 1 * $("#tbdShowNumber td.onNum").length));
    }else{
      $("#tbdShowNumber").html('<tr><td colspan="4" class="c2"> -- </td></tr>');
       $("#sendNumTotal").text('0');
       $("#sendMonNumTotal").text('0');
    }
    $(this).removeAttr("disabled");
  }); 
  
  // clear
  $("input:reset").click(function(){
    $(".tblQPG label").removeClass('noClk');   
  }); 
  
  // btnCancel
  $("#btnCancel").click(function(){
    $("#tbdShowNumber").empty();
    $("#sendNumTotal").text('0');
    $("#sendMonNumTotal").text('0');
    $("#setMomney").val('0');
    $("#totalNumCount").text('0');
  });
  
  // btnSent    
  $("#btnSent").click(function(){
    var setMon = $('#setMomney').val(); 
    if(setMon <  _betmin){ alert(_err01); $('#setMomney').focus(); return false; }
    var l = $("#tbdShowNumber td.onNum").length;
    if(l < 1){ alert(_err02); return false; }
    $(this).attr('disabled');
    var str = '';
    $("#tbdShowNumber td.onNum input:hidden").val(setMon);
    $(this).attr("disabled", "disabled");
    $("input#disk").attr("value",$("select#slcDisk option:selected").val());
    $("#frmSubData").submit();
  });
  
  // btn change face to number
  $("#chgFace2Num").click(function(){
    $(".autoLab2 input").removeAttr('checked').parent('label').removeClass('noClk');
    var oChkG = $('#tblQPG02 input:checkbox:checked');
    var oChkGL = oChkG.length;
    var i = 0;  
    for(i = 0; i < oChkGL; i++){ getFace2Num(oChkG[i].value); oChkG[i].click(); }
  });
  
  // time of now
  $("span#showNowTime").showNowTime();
  
});                                                                                                                                                                        

// get the bet name of number
function getBetNumName(pStr, gStr, aChkArr){
  var uTitle = new Array();uTitle['0_1'] = '第一球';uTitle['0_2'] = '第二球';uTitle['0_3'] = '第三球';uTitle['0_4'] = '第四球';uTitle['0_5'] = '第五球';uTitle['1_1'] = '第一球单双';uTitle['1_2'] = '第二球单双';uTitle['1_3'] = '第三球单双';uTitle['1_4'] = '第四球单双';uTitle['1_5'] = '第五球单双';uTitle['2_1'] = '第一球大小';uTitle['2_2'] = '第二球大小';uTitle['2_3'] = '第三球大小';uTitle['2_4'] = '第四球大小';uTitle['2_5'] = '第五球大小';uTitle['3_0'] = '总单双';uTitle['4_0'] = '总大小'; uTitle['5_0'] = '龙虎和';uTitle['6_0'] = '后三';uTitle['6_1'] = '中三';uTitle['6_2'] = '前三';
  var titleStrArr = gStr.split("_");  if(titleStrArr.length != 2){ return '- error -'; } var bettype = titleStrArr[0];  var subid = pStr;  var num = titleStrArr[1]; var titleIdx = bettype + '_' + subid; var titleStr = '<input type="hidden" name="b_' + subid + '_' + bettype + '_' + num + '" value="" />【' + uTitle[titleIdx] + '】';
  switch(bettype) { 	case '0':  return titleStr + num; 	break; 	case '1':  var tempArr = [ '', '单', '双']; 	return titleStr + tempArr[num]; 	break; 	case '2':  var tempArr = [ '', '大', '小']; 	return titleStr + tempArr[num]; 	break; 	case '3':  var tempArr = ['', '总单', '总双']; 	return titleStr + tempArr[num]; 	break; 	case '4':  var tempArr = ['', '总大', '总小']; 	return titleStr + tempArr[num]; 	break; 	case '5':  var tempArr = ['', '龙', '虎', '和']; 	return titleStr + tempArr[num]; 	break; 	case '6':  var tempArr = ['豹子', '顺子', '对子', '半顺', '杂六']; 	return titleStr + tempArr[num]; 	break; }
  return '-null-';
}; 

// filter the num arr
function getFace2Num(fTypeStr){
  var myBtnId = fTypeStr.split("_");
  if(myBtnId.length != 2 ){ return ;}
  var i = 0;
  var oObj = $(".autoLab2 input:not(:checked)");
  var attVal1 = '';
  var attVal2 = '';
  var sNumIdx = 1;
  switch(myBtnId[0]){
    case '1':
      var typeCharNum = myBtnId[1] % 2;
      oObj.filter(function(idx, ele){
          var myN = $(ele).val().split("_");
          var sNum = myN[sNumIdx];
          return (sNum % 2 == typeCharNum);
      }).click();
      break;
    case '2':
      var typeCharNum = myBtnId[1] -1;
      oObj.filter(function(idx, ele){
          var myN = $(ele).val().split("_");
          var sNum = myN[sNumIdx];
          if(typeCharNum){ return (sNum < 5); }
          else{ return (sNum > 4); }
      }).click();
      break;
  }
}

$.fn.showNowTime = function(){
  var thisObj = $(this);
  var str = "时间：<b>"+ _now.getDate() +"【"+( _now.getHours() )+":"+( _now.getMinutes())+":"+( _now.getSeconds())+"】</b>";
  if(_timestap >=0){
    timediff = _timestap;
    days = Math.floor(timediff / (86400));   // 60 * 60 * 24
  	timediff -= days * (86400);
  	hours = Math.floor(timediff / (3600));  // 60 * 60
  	timediff -= hours * (3600);	
  	mins = Math.floor(timediff / (60));
  	timediff -= mins * (60);	
  	secs = Math.floor(timediff);
    str += "，距离【第" + _round + "期】截止下注时间还有 ";
    if (days > 0 ) str += "<b>" + days + "</b> 天 ";
    if (hours > 0) str += "<b>" + hours + "</b> 小时 ";
    if (mins > 0) str += "<b>" + mins + "</b> 分钟 ";
    str += "<b>" + secs + "</b> 秒 ";
    _timestap-=1;
  }else{
    str += "<font color=red><b>【" + _gametypeStr + "】已经截止下注！</b></font>";
    setTimeout('parent.mainFrame.location.reload()',1000);
    setTimeout('parent.leftFrame.location.reload()',1000);
  }
  $(this).html(str);
  _now.setTime(_now.getTime() + 1000);
  setTimeout(function(){$(thisObj).showNowTime();},1000);
}; 
